package com.westa.smallshop.common;


import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.afollestad.materialdialogs.MaterialDialog;
import com.squareup.leakcanary.RefWatcher;
import com.westa.smallshop.SmallShopApplication;
import com.westa.smallshop.util.SingleToast;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public abstract class BaseFragment extends Fragment {


    public static final int REQUEST_DETAIL_CODE = 1111;

    @Nullable
    private View mFragmentView;

    private SingleToast mSingleToast;

    private MaterialDialog mMaterialDialog;

    private Unbinder mUnbinder;

    public BaseFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initDefaultSingleToast();
    }

    @Nullable
    @Override
    final public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                   Bundle savedInstanceState) {
        @LayoutRes
        int layoutRes = getCreateViewLayoutRes();
        if (layoutRes != 0) {

            if (mFragmentView == null) {

                mFragmentView = inflater.inflate(layoutRes, container, false);
                mUnbinder = ButterKnife.bind(this, mFragmentView);
                initView();
            }
            return mFragmentView;
        }

        return null;
    }

    public abstract void initView();

    @LayoutRes
    public int getCreateViewLayoutRes() {

        return 0;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mFragmentView != null) {
            ((ViewGroup) mFragmentView.getParent()).removeView(mFragmentView);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mUnbinder.unbind();
        RefWatcher refWatcher = SmallShopApplication.getRefWatcher(getActivity());
        refWatcher.watch(this);
    }

    private void initDefaultSingleToast() {

        mSingleToast = new SingleToast(getActivity().getApplicationContext());
    }


    public void showSingleToast(@StringRes int hintRes) {

        mSingleToast.showBottomToast(hintRes);
    }

    public void showSingleToast(String hintStr) {
        if (!TextUtils.isEmpty(hintStr)) {
            mSingleToast.showBottomToast(hintStr);
        }
    }

    public void showDialogProgress(@StringRes int hintRes, DialogInterface.OnDismissListener onDismissListener) {

        mMaterialDialog = new MaterialDialog.Builder(getContext())
                .content(hintRes)
                .progress(true, 0)
                .dismissListener(onDismissListener)
                .show();

    }

    public void dismissDialogProgress() {
        if (mMaterialDialog != null && mMaterialDialog.isShowing()) {
            mMaterialDialog.dismiss();
        }
    }

}
